package zucc.lcj.android_finalwork.me;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import zucc.lcj.android_finalwork.LoginActivity;
import zucc.lcj.android_finalwork.MyDatabaseHelper;
import zucc.lcj.android_finalwork.R;


public class ChangePwdActivity extends AppCompatActivity {
    EditText oldpwd,newpwd1,newpwd2;
    Button back,go;
    private String oldpwds="",newpwds1="",newpwds2="";
    private String oldpwdright="";
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepwd);
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);

        oldpwd = (EditText) findViewById(R.id.change_pwd_old);
        newpwd1 = (EditText) findViewById(R.id.change_pwd_new1);
        newpwd2 = (EditText) findViewById(R.id.change_pwd_new2);
        back = (Button) findViewById(R.id.change_pwd_back);
        go = (Button) findViewById(R.id.change_pwd_yes);

        oldpwds=oldpwd.getText().toString();
        newpwds1=newpwd1.getText().toString();
        newpwds2=newpwd2.getText().toString();

        go.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                oldpwds=oldpwd.getText().toString();
                newpwds1=newpwd1.getText().toString();
                newpwds2=newpwd2.getText().toString();
                System.out.println("oldpwds"+oldpwds);
                System.out.println("newpwds1"+newpwds1);
                System.out.println("newpwds2"+newpwds2);
                if (oldpwds.equals("")){
                    AlertDialog alertDialog2 = new AlertDialog.Builder(ChangePwdActivity.this)
                            .setTitle("提示")
                            .setMessage("请输入原密码")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }//添加"Yes"按钮
                            })
                            .create();
                    alertDialog2.show();
                }
                if (newpwds1.equals("")){
                    AlertDialog alertDialog2 = new AlertDialog.Builder(ChangePwdActivity.this)
                            .setTitle("提示")
                            .setMessage("请输入新密码")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }//添加"Yes"按钮
                            })
                            .create();
                    alertDialog2.show();
                }
                if (newpwds2.equals("")){
                    AlertDialog alertDialog2 = new AlertDialog.Builder(ChangePwdActivity.this)
                            .setTitle("提示")
                            .setMessage("请确认新密码")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }//添加"Yes"按钮
                            })
                            .create();
                    alertDialog2.show();
                }
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name = LoginActivity.global_login_user;
                Cursor cursor = db.rawQuery("select * from UserInfo where user_name=?",new String[]{name});
                if (cursor.moveToFirst()){
                    oldpwdright = cursor.getString(cursor.getColumnIndex("user_pwd"));
                }
                if (!oldpwds.equals(oldpwdright)){
                    Toast.makeText(getApplicationContext(), "原密码错误", Toast.LENGTH_SHORT).show();
                }
                else {
                    if (!newpwds1.equals(newpwds1)){
                        Toast.makeText(getApplicationContext(), "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        db.execSQL("update UserInfo set user_pwd=? where UserInfo.user_name=?",new String[]{newpwds1,LoginActivity.global_login_user});
                        AlertDialog alertDialog2 = new AlertDialog.Builder(ChangePwdActivity.this)
                                .setTitle("恭喜你！")
                                .setMessage("密码修改成功")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent();
                                        intent.setClass(ChangePwdActivity.this, MeActivity.class);
                                        startActivity(intent);
                                    }//添加"Yes"按钮
                                })
                                .create();
                        alertDialog2.show();
                    }
                }

            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(ChangePwdActivity.this, SetupActivity.class);
                startActivity(intent);
            }
        });
    }
}